<HTML>
<BODY BGCOLOR="white">
<PRE>
<FONT color="green">001</FONT>    // Copyright 2007 Waterken Inc. under the terms of the MIT X license<a name="line.1"></a>
<FONT color="green">002</FONT>    // found at http://www.opensource.org/licenses/mit-license.html<a name="line.2"></a>
<FONT color="green">003</FONT>    package org.waterken.factorial;<a name="line.3"></a>
<FONT color="green">004</FONT>    <a name="line.4"></a>
<FONT color="green">005</FONT>    import org.ref_send.promise.Eventual;<a name="line.5"></a>
<FONT color="green">006</FONT>    import org.ref_send.promise.Promise;<a name="line.6"></a>
<FONT color="green">007</FONT>    <a name="line.7"></a>
<FONT color="green">008</FONT>    /**<a name="line.8"></a>
<FONT color="green">009</FONT>     * A tail recursive factorial implementation.<a name="line.9"></a>
<FONT color="green">010</FONT>     */<a name="line.10"></a>
<FONT color="green">011</FONT>    public final class<a name="line.11"></a>
<FONT color="green">012</FONT>    Factorial {<a name="line.12"></a>
<FONT color="green">013</FONT>        private Factorial() { /* no instance interface */ }<a name="line.13"></a>
<FONT color="green">014</FONT>        <a name="line.14"></a>
<FONT color="green">015</FONT>        /**<a name="line.15"></a>
<FONT color="green">016</FONT>         * Computes a factorial.<a name="line.16"></a>
<FONT color="green">017</FONT>         * @param _ eventual operator<a name="line.17"></a>
<FONT color="green">018</FONT>         * @param n &lt;code&gt;&amp;gt;= 0&lt;/code&gt;<a name="line.18"></a>
<FONT color="green">019</FONT>         * @return promise for the factorial of &lt;code&gt;n&lt;/code&gt;<a name="line.19"></a>
<FONT color="green">020</FONT>         */<a name="line.20"></a>
<FONT color="green">021</FONT>        static public Promise&lt;Integer&gt;<a name="line.21"></a>
<FONT color="green">022</FONT>        make(final Eventual _, final int n) {<a name="line.22"></a>
<FONT color="green">023</FONT>            if (n &lt; 0) { return Eventual.reject(new Exception()); }<a name="line.23"></a>
<FONT color="green">024</FONT>            /*<a name="line.24"></a>
<FONT color="green">025</FONT>             * We'll simulate tail recursive calls by doing eventual invocations of<a name="line.25"></a>
<FONT color="green">026</FONT>             * our loop function. Since the call is eventual, the current stack<a name="line.26"></a>
<FONT color="green">027</FONT>             * frame is popped right away and the loop's stack frame isn't created<a name="line.27"></a>
<FONT color="green">028</FONT>             * until the next event loop turn. The same trick is used inside the<a name="line.28"></a>
<FONT color="green">029</FONT>             * loop itself. The chain of promises produced by these calls also<a name="line.29"></a>
<FONT color="green">030</FONT>             * collapses during each event loop turn, so that there's always only<a name="line.30"></a>
<FONT color="green">031</FONT>             * a constant number of objects referenced. The efficiency of this<a name="line.31"></a>
<FONT color="green">032</FONT>             * probably isn't as good as in a VM designed for tail recursion, but<a name="line.32"></a>
<FONT color="green">033</FONT>             * may be good enough for some purposes.<a name="line.33"></a>
<FONT color="green">034</FONT>             */<a name="line.34"></a>
<FONT color="green">035</FONT>            final Recursion loop_ = _._(new Recursion() {<a name="line.35"></a>
<FONT color="green">036</FONT>                public Promise&lt;Integer&gt;<a name="line.36"></a>
<FONT color="green">037</FONT>                apply(final Recursion tail_, final int i, final int acc) {<a name="line.37"></a>
<FONT color="green">038</FONT>                    if (i == 0) { return Eventual.ref(acc); }<a name="line.38"></a>
<FONT color="green">039</FONT>                    return tail_.apply(tail_, i - 1, i * acc);<a name="line.39"></a>
<FONT color="green">040</FONT>                }<a name="line.40"></a>
<FONT color="green">041</FONT>            });<a name="line.41"></a>
<FONT color="green">042</FONT>            return loop_.apply(loop_, n, 1);<a name="line.42"></a>
<FONT color="green">043</FONT>        }<a name="line.43"></a>
<FONT color="green">044</FONT>    <a name="line.44"></a>
<FONT color="green">045</FONT>        /**<a name="line.45"></a>
<FONT color="green">046</FONT>         * The inner loop of a tail recursive factorial implementation.<a name="line.46"></a>
<FONT color="green">047</FONT>         */<a name="line.47"></a>
<FONT color="green">048</FONT>        static public interface<a name="line.48"></a>
<FONT color="green">049</FONT>        Recursion { Promise&lt;Integer&gt; apply(Recursion tail_, int i, int acc); }<a name="line.49"></a>
<FONT color="green">050</FONT>    }<a name="line.50"></a>




























































</PRE>
</BODY>
</HTML>
